<template>
  <el-card v-loading="loading">
    <div style="margin-bottom: 20px; font-size: 20px;color: #606266;">功能入口设置
      <el-button type="primary" style="float: right" @click="handleAdd">新增功能</el-button>
    </div>

    <el-tabs v-model="active" v-loading="loading">
      <el-tab-pane label="可选择功能" name="enable">
        <el-table
          :data="list"
          style="width: 100%"
        >
          <el-table-column
            prop="name"
            label="可选功能名称"
          />

          <el-table-column
            prop="description"
            label="说明"
          />

          <el-table-column
            prop="code"
            label="功能编码"
          />

          <el-table-column
            prop="featureCode"
            label="关联功能集"
          >
            <template slot-scope="scope">
              <div v-for="item in scope.row.sysFeatureIdList" :key="item">
                <span v-if="fmap[item]">
                  {{ fmap[item].name }} - {{ fmap[item].code }}
                </span>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="pagePath"
            label="路径"
          />
          <el-table-column
            prop="option"
            label="操作"
          >
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="text" size="small" @click="handleChangeStatus(scope.row.id, 0)">弃用</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>

      <el-tab-pane label="已弃用功能" name="disable">
        <el-table
          :data="disableList"
          style="width: 100%"
        >

          <el-table-column
            prop="name"
            label="可选功能名称"
          />

          <el-table-column
            prop="description"
            label="说明"
          />

          <el-table-column
            prop="code"
            label="功能编码"
          />

          <el-table-column
            prop="featureCode"
            label="关联功能集"
          >
            <template slot-scope="scope">
              <div v-for="item in scope.row.sysFeatureIdList" :key="item">
                <span v-if="fmap[item]">
                  {{ fmap[item].name }} - {{ fmap[item].code }}
                </span>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="pagePath"
            label="路径"
          />
          <el-table-column
            prop="option"
            label="操作"
          >
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="text" size="small" @click="handleChangeStatus(scope.row.id, 1)">设置为可选择</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <feature-entrance-dialog ref="featureEntrance" />
  </el-card>
</template>

<script>
/* eslint-disable no-console */
import FeatureEntranceDialog from '../components/FeatureEntranceDialog'
import { changeStatus, getList } from '@/api/wxaEntrance'
import { queryFeatureList } from '@/api/feature'

export default {
  name: 'WxaTplFeatureEntrance',
  components: {
    FeatureEntranceDialog
  },
  props: {
    msg: String
  },
  data() {
    return {
      loading: false,
      active: 'enable',
      list: [],
      fmap: {},
      disableList: []
    }
  },
  computed: {},
  mounted() {
    queryFeatureList(2, { pageNum: 1, pageSize: 1000 }).then(res => {
      const featureList = res.data.list
      let i = 0
      const fmap = {}
      for (i = 0; i < featureList.length; i++) {
        const item = featureList[i]
        fmap[item.id] = item
      }
      this.fmap = fmap
    })
    this.fetchData()
  },

  methods: {
    handleAdd() {
      this.$refs.featureEntrance.add().then(() => {
        this.fetchData()
      })
    },
    handleEdit(entrance) {
      this.$refs.featureEntrance.edit(entrance).then(() => {
        this.fetchData()
      })
    },
    fetchData() {
      getList(0, 1).then(res => {
        this.list = res.data
      })

      getList(0, 0).then(res => {
        this.disableList = res.data
      })
    },
    handleChangeStatus(id, status) {
      this.loading = true
      changeStatus({ id, status }).then(res => {
        this.loading = false
        this.$notify({ title: '成功', message: '操作成功', type: 'success', duration: 2000 })
        this.fetchData()
      }).catch(res => {
        this.loading = false
      })
    }
  }
}
</script>

